<template>
  <Dialog v-model="dialogVisible" :title="title" width="80%" :minHeight="900">
    <ExcelParser
      :tips="tips"
      ref="excelParserRef"
      :importApi="DeptApi.deptImport"
      :targetTableColumns="importColumns"
      :templateName="title"
    />
    <template #footer>
      <el-button type="primary" @click="onBatchImport()" :loading="importLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>

<script setup lang="ts" name="ImportDailyBreakClassDialog">
import { useValidator } from '@/hooks/web/useValidator'
const { required } = useValidator()
// import * as DailyBreakClassApi from '@/api/process/dailyBreakClass'
import * as DeptApi from '@/api/system/dept'
import { ITargetTableColumbnItem } from '@/components/ExcelParser/src/type'
import { DICT_TYPE, getDictValuesStr } from '@/utils/dict'
// import { CommonStatusEnum } from '@/utils/constants'
const props = defineProps({
  planId: null,
  schoolId: null
})

const importLoading = ref(false)
const excelParserRef = ref()

const title = ref('导入部门')
const dialogVisible = ref(false)
const tips = ref([
  '状态: ' + getDictValuesStr(DICT_TYPE.COMMON_STATUS),
  '机构级次id: ' + getDictValuesStr(DICT_TYPE.PROCESS_APPROVE_LEADER_DEPT_LEVEL)
])
const importColumns = ref<ITargetTableColumbnItem[]>([
  {
    label: '组织机构',
    field: 'name',
    rules: [required('组织机构不能为空')]
  },
  {
    label: '上级部门名',
    field: 'parentName',
    rules: [required('上级部门名不能为空')]
  },
  {
    label: '显示顺序',
    field: 'sort'
  },
  {
    label: '负责人编号',
    field: 'leaderUserId',
    type: 'number' //表格内容数据类型
  },
  {
    label: '联系电话',
    field: 'phone'
  },
  {
    label: '邮箱',
    field: 'email'
  },
  {
    label: '状态',
    field: 'status',
    parseRule: {
      dictType: DICT_TYPE.COMMON_STATUS,
      defaultValue: 0
    },
    rules: [required('状态不能为空')]
  },
  {
    label: '机构级次id',
    field: 'levelId',
    rules: [required('机构级次id不能为空')],
    parseRule: {
      dictType: DICT_TYPE.PROCESS_APPROVE_LEADER_DEPT_LEVEL,
      defaultValue: ''
    }
    // rules: [required('学校不能为空')]
  }
])

const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调

const onBatchImport = async () => {
  importLoading.value = true
  const obj = { schoolId: props.schoolId, planId: props.planId }
  const isSuccess = await excelParserRef.value.onSubmit(obj)
  if (isSuccess) {
    importLoading.value = false
    ElMessage({
      message: '导入成功',
      type: 'success',
      duration: 1000,
      onClose: () => {
        // importLoading.value = false
        dialogVisible.value = false
      }
    })
    emit('success')
  } else {
    importLoading.value = false
    return
  }
  importLoading.value = false
  dialogVisible.value = false
}

/** 打开弹窗 */
const open = async () => {
  dialogVisible.value = true
  console.log('open')
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗
</script>
